package com.qf.kdxf.service.impl;

import com.google.gson.Gson;
import com.qf.kdxf.entity.identify.JsonParse;
import com.qf.kdxf.service.SpeechIdentifyService;
import com.qf.kdxf.sign.HttpUtil;
import com.qf.kdxf.sign.LfasrSignature;
import com.qf.kdxf.util.IdentifyUtil;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;

/**
 * @Author 明廷盛
 * @Date 2025/6/16 11:29
 * @Description
 */
@Service
public class AudioToTextServiceImpl implements SpeechIdentifyService {
    private static final String HOST = "https://raasr.xfyun.cn";
    private static final String appid = "ced63297";
    private static final String keySecret = "0ce3135e285ae4fa9c63787ecf4858b3";
    private static final Gson gson = new Gson();

    @Override
    public String upload(String filePath) {
        String response = null;
        try {
            HashMap<String, Object> map = new HashMap<>(16);
            File audio = new File(filePath);
            String fileName = audio.getName();
            long fileSize = audio.length();
            map.put("appId", appid);
            map.put("fileSize", fileSize);
            map.put("fileName", fileName);
            map.put("duration", "200");
            LfasrSignature lfasrSignature = new LfasrSignature(appid,
                    keySecret);
            map.put("signa", lfasrSignature.getSigna());
            map.put("ts", lfasrSignature.getTs());
            String paramString = HttpUtil.parseMapToPathParam(map);
            System.out.println("upload paramString:" + paramString);
            String url = HOST + "/v2/api/upload" + "?" + paramString;
            System.out.println("upload_url:" + url);
            response = HttpUtil.iflyrecUpload(url, new FileInputStream(audio));
            System.out.println("upload response:" + response);
        } catch (Exception e) {
        }
        return response;
    }

    @Override
    public String getResult(String orderId) {
        String write = null;
        try {
            HashMap<String, Object> map = new HashMap<>(16);
            map.put("orderId", orderId);
            LfasrSignature lfasrSignature = new LfasrSignature(appid, keySecret);
            map.put("signa", lfasrSignature.getSigna());
            map.put("ts", lfasrSignature.getTs());
            map.put("appId", appid);
            map.put("resultType", "transfer,predict");
            String paramString = HttpUtil.parseMapToPathParam(map);
            String url = HOST + "/v2/api/getResult" + "?" + paramString;
            System.out.println("\nget_result_url:" + url);
            while (true) {
                String response = HttpUtil.iflyrecGet(url);
                JsonParse jsonParse = gson.fromJson(response, JsonParse.class);
                if (jsonParse.getContent().getOrderInfo().getStatus() == 4 ||
                        jsonParse.getContent().getOrderInfo().getStatus() == -1) {
                    System.out.println("订单完成:" + response);
                    write = IdentifyUtil.write(response);
                    return write;
                } else {
                    System.out.println("进行中...，状态为:" +
                            jsonParse.getContent().getOrderInfo().getStatus());
//建议使用回调的方式查询结果，查询接口有请求频率限制
                    Thread.sleep(7000);
                }
            }
        } catch (Exception e) {
        }
        return write;
    }
}


